package com.aj.activity;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.RelativeLayout;

import com.aj.activity.view.ImageControl;
import com.aj.databases.entry.AlbumPhotoInfo;
import com.aj.utils.PortraitLoaderUtil;

public class PhotoInfoActivity extends BaseActivity {
	private ArrayList<View> listViews = null;
	private ViewPager pager;
	private ViewPageAdapter adapter;
	private RelativeLayout bottom_tools;
	private int count;
	private int allCount;
	private int index = 1;
	private List<AlbumPhotoInfo> albumPhotoInfoList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_photo_info);
		setActionBackgourdResource(R.drawable.photo_info_bar);
		initView();
	}

	private void initView() {
		pager = (ViewPager) findViewById(R.id.viewpager);
		bottom_tools = (RelativeLayout) findViewById(R.id.bottom_tools);
		pager.setOnPageChangeListener(pageChangeListener);
		index = getIntent().getIntExtra("index", 0);
		albumPhotoInfoList = (List<AlbumPhotoInfo>) getIntent()
				.getSerializableExtra("albumList");
		bottom_tools.setVisibility(View.INVISIBLE);
		if (albumPhotoInfoList != null && albumPhotoInfoList.size() > 0) {
			for (int i = 0; i < albumPhotoInfoList.size(); i++) {
				initListViews(albumPhotoInfoList.get(i));//
			}
			allCount = albumPhotoInfoList.size();
			setActionBarTitle((index + 1) + "/" + allCount);
			adapter = new ViewPageAdapter(listViews);// 构造adapter
			pager.setAdapter(adapter);
			pager.setCurrentItem(index);
		}

	}

	private void initListViews(final AlbumPhotoInfo albumPhotoInfo) {
		if (listViews == null) {
			listViews = new ArrayList<View>();
		}
		final ImageControl imageControl = new ImageControl(
				PhotoInfoActivity.this);
		imageControl.setBackgroundColor(0xff000000);
		imageControl.post(new Runnable() {
			@Override
			public void run() {
				PortraitLoaderUtil.loadSrc(PhotoInfoActivity.this,
						imageControl, null, albumPhotoInfo.getAlbumUrl(),
						R.drawable.nomal_photo, R.drawable.nomal_photo,
						imageControl.getMeasuredWidth(),
						imageControl.getMeasuredHeight());
			}
		});
		imageControl.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT));
		listViews.add(imageControl);// 添加view
	}

	private OnPageChangeListener pageChangeListener = new OnPageChangeListener() {

		@Override
		public void onPageSelected(int arg0) {// 页面选择响应函数
			count = arg0;
			setActionBarTitle((arg0 + 1) + "/" + allCount);
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {// 滑动中。。。

		}

		@Override
		public void onPageScrollStateChanged(int arg0) {// 滑动状态改变

		}
	};

	class ViewPageAdapter extends PagerAdapter {

		private ArrayList<View> listViews;// content

		private int size;// 页数

		public ViewPageAdapter(ArrayList<View> listViews) {// 构造函数
															// 初始化viewpager的时候给的一个页面
			this.listViews = listViews;
			size = listViews == null ? 0 : listViews.size();
		}

		public void setListViews(ArrayList<View> listViews) {// 自己写的一个方法用来添加数据
			this.listViews = listViews;
			size = listViews == null ? 0 : listViews.size();
		}

		@Override
		public int getCount() {// 返回数量
			return size;
		}

		@Override
		public int getItemPosition(Object object) {
			return POSITION_NONE;
		}

		@Override
		public void destroyItem(View arg0, int arg1, Object arg2) {// 销毁view对象
			((ViewPager) arg0).removeView(listViews.get(arg1 % size));
		}

		@Override
		public void finishUpdate(View arg0) {
		}

		@Override
		public Object instantiateItem(View arg0, int arg1) {// 返回view对象
			try {
				((ViewPager) arg0).addView(listViews.get(arg1 % size), 0);

			} catch (Exception e) {
			}
			return listViews.get(arg1 % size);
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

	}
}
